package com.yocard.yxyd.ui.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatEditText;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yocard.yxyd.others.EditTextWatcher;
import com.yocard.yxyd.utils.util.KeyboardUtils;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.yocard.yxyd.R;
import com.yocard.yxyd.http.model.request.OrderListRequest;
import com.yocard.yxyd.others.DecimalDigitsInputFilter;
import com.yocard.yxyd.utils.BigDecimalUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zhy.view.flowlayout.TagView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created on 2019/4/10.
 */
public class OrderDrawerView extends FrameLayout {

    @BindView(R.id.flow_zhiye)
    TagFlowLayout flowZhiye;
    @BindView(R.id.flow_ziliao)
    TagFlowLayout flowZiliao;
    @BindView(R.id.et_start_amount)
    AppCompatEditText etStartAmount;
    @BindView(R.id.et_end_amount)
    AppCompatEditText etEndAmount;
    @BindView(R.id.et_in_start)
    AppCompatEditText etInStart;
    @BindView(R.id.et_in_end)
    AppCompatEditText etInEnd;
    @BindView(R.id.et_age_start)
    AppCompatEditText etAgeStart;
    @BindView(R.id.et_age_end)
    AppCompatEditText etAgeEnd;


    private List<ValueCallback<OrderListRequest>> callbacks = new ArrayList<>();
    protected String zhiye;
    protected OrderListRequest request;

    public OrderDrawerView(@NonNull Context context) {
        this(context, null);
    }

    public OrderDrawerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OrderDrawerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_order_drawer, this);
        ButterKnife.bind(this);
        etStartAmount.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(1)});
        etEndAmount.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(1)});
        etInStart.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(1)});
        etInEnd.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(1)});
        etStartAmount.addTextChangedListener(new EditTextWatcher(etStartAmount));
        etEndAmount.addTextChangedListener(new EditTextWatcher(etEndAmount));
        etInStart.addTextChangedListener(new EditTextWatcher(etInStart));
        etInEnd.addTextChangedListener(new EditTextWatcher(etInEnd));
        request = new OrderListRequest();
        initZhiye();
        initZiliao();
    }

    @OnClick({R.id.tv_reset, R.id.tv_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ok:
                ok();
                break;
            case R.id.tv_reset:
                resetValues();
                if (callbacks != null && callbacks.size() > 0) {
                    for (ValueCallback<OrderListRequest> callback : callbacks) {
                        callback.onReceiveValue(null);
                    }
                }
                break;
        }
    }

    public void resetValues() {
        request = new OrderListRequest();
        etStartAmount.setText("");
        etEndAmount.setText("");
        etInStart.setText("");
        etInEnd.setText("");
        etAgeStart.setText("");
        etAgeEnd.setText("");
        zhiye = "";
        flowZhiye.onChanged();
        flowZiliao.onChanged();
    }

    private void ok() {

        String startAmount = etStartAmount.getText().toString().trim();
        String endAmount = etEndAmount.getText().toString().trim();
        String inStart = etInStart.getText().toString().trim();
        String inEnd = etInEnd.getText().toString().trim();
        String ageStart = etAgeStart.getText().toString().trim();
        String ageEnd = etAgeEnd.getText().toString().trim();
        if (startAmount.equals(".") || endAmount.equals(".")) {
            ToastUtils.showShort("请正确输入贷款金额");
            return;
        }
        if (inStart.equals(".") || inEnd.equals(".")) {
            ToastUtils.showShort("请正确输入月收入");
            return;
        }

        if (BigDecimalUtil.compareTo(startAmount, endAmount) == 1) {
            ToastUtils.showShort("请正确输入金额范围");
            return;
        }

        if (BigDecimalUtil.compareTo(inStart, inEnd) == 1) {
            ToastUtils.showShort("请正确输入月收入范围");
            return;
        }

        if (BigDecimalUtil.compareTo(ageStart, ageEnd) == 1) {
            ToastUtils.showShort("请正确输入年龄范围");
            return;
        }

        if(BigDecimalUtil.compareTo(startAmount, "0.5") == -1
                || BigDecimalUtil.compareTo(endAmount, "0.5") == -1) {
            ToastUtils.showShort("最低金额不能低于0.5万");
            return;
        }

        request.setStart_amount(startAmount);
        request.setEnd_amount(endAmount);
        request.setStart_price(inStart);
        request.setEnd_price(inEnd);
        request.setStart_age(ageStart);
        request.setEnd_age(ageEnd);
        request.setCareer(zhiye);
        if (callbacks != null && callbacks.size() > 0) {
            for (ValueCallback<OrderListRequest> callback : callbacks) {
                callback.onReceiveValue(request);
            }
        }

//        resetValues();
    }

    public void setSelectListener(ValueCallback<OrderListRequest> callback) {
        callbacks.add(callback);
    }

    private void initZiliao() {
        String[] ziliaoArray = getResources().getStringArray(R.array.ziliao);
        flowZiliao.setAdapter(new TagAdapter<String>(ziliaoArray) {

            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_tv, flowZiliao, false);
                tv.setText(s);
                return tv;
            }
        });
        flowZiliao.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                KeyboardUtils.hideSoftInput(OrderDrawerView.this);
//                    switch (position) {
//                        case 0:
//                            request.setIs_social_insurance(request.getIs_social_insurance() == 0?1:0);
//                            break;
//                        case 1:
//                            request.setIs_fund(request.getIs_fund() == 0 ? 1 : 0);
//                            break;
//                        case 2:
//                            request.setIs_car(request.getIs_car() == 0?1:0);
//                            break;
//                        case 3:
//                            request.setIs_house(request.getIs_house() == 0?1:0);
//                            break;
//                        case 4:
//                            request.setIs_zhima(request.getIs_zhima() == 0?1:0);
//                            break;
//                        case 5:
//                            request.setIs_weili(request.getIs_weili() == 0?1:0);
//                            break;
//                        case 6:
//                            request.setIs_bxbd(request.getIs_bxbd() == 0?1:0);
//                            break;
//                    }

                TagView tagView = (TagView) view;
                if (tagView.isChecked()) {
                    switch (position) {
                        case 0:
                            request.setIs_social_insurance(1);
                            break;
                        case 1:
                            request.setIs_fund(1);
                            break;
                        case 2:
                            request.setIs_car(1);
                            break;
                        case 3:
                            request.setIs_house(1);
                            break;
                        case 4:
                            request.setIs_zhima(1);
                            break;
                        case 5:
                            request.setIs_weili(1);
                            break;
                        case 6:
                            request.setIs_bxbd(1);
                            break;
                    }
                } else {
                    switch (position) {
                        case 0:
                            request.setIs_social_insurance(0);
                            break;
                        case 1:
                            request.setIs_fund(0);
                            break;
                        case 2:
                            request.setIs_car(0);
                            break;
                        case 3:
                            request.setIs_house(0);
                            break;
                        case 4:
                            request.setIs_zhima(0);
                            break;
                        case 5:
                            request.setIs_weili(0);
                            break;
                        case 6:
                            request.setIs_bxbd(0);
                            break;
                    }
                }

                return false;
            }
        });
    }

    private void initZhiye() {
        String[] zhiyeArray = getResources().getStringArray(R.array.zhiye);
        String[] zhiyeFied = getResources().getStringArray(R.array.zhiye_fied);
        flowZhiye.setAdapter(new TagAdapter<String>(zhiyeArray) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_tv, flowZhiye, false);
                tv.setText(s);
                return tv;
            }
        });
        flowZhiye.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                TagView tv = (TagView) view;
                if (tv.isChecked()) {
                    zhiye = zhiyeFied[position];
                } else {
                    zhiye = "";
                }
                KeyboardUtils.hideSoftInput(OrderDrawerView.this);
                return false;
            }
        });
    }
}
